package nml.webmail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class sina_cn extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {

		try {
			String url = "http://mail.sina.com.cn/";
			dr.get(url);

			dr.findElement(By.id("freename")).sendKeys(uid);
			dr.findElement(By.id("freepassword")).sendKeys(password);
			dr.findElement(By.className("loginBtn")).click();

			waitWebElement(By.linkText("写信")).click();

			content = StringUtils.remove(content, '\r');
			content = StringUtils.remove(content, '\n');

			content = StringEscapeUtils.escapeJavaScript(content);

			loadJQuery();

			String js = loadJS();

			js = StringUtils.replace(js, "$content", content);
			js = StringUtils.replace(js, "$subject", subject);
			js = StringUtils.replace(js, "$to", to);

			waitWebElement(By.id("checkbox2"));

			executeScript(js);

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "nml186@sina.cn";
			String password = "Wen101577474";
			String to = "o21x@qq.com";
			String subject = "nini";
			String content = FileUtils.readFileToString(new File("a.txt"));

			new sina_cn().send(uid, password, to, subject, content);
			Thread.sleep(20000);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
